// 备份原index.js文件
// 这是原始文件的备份，以防需要恢复
import { createRouter, createWebHistory } from 'vue-router'
/* Layout */
import Layout from '@/layout'
import apiTest from '@/views/qep/ApiTest'

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * roles: ['admin', 'common']       // 访问路由的角色权限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
 * meta : {
    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */

// 公共路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: () => import('@/views/index'),
        name: 'Index',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'profile',
        component: () => import('@/views/system/user/profile/index'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user' }
      }
    ]
  },
  {
    path: '/process-api-test',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/qep/process/api-test.vue'),
        name: 'ProcessApiTest',
        meta: { title: '工序API测试', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/process-type-api-test',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/qep/processType/api-test.vue'),
        name: 'ProcessTypeApiTest',
        meta: { title: '工序类型API测试', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/system-info',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/qep/system-info.vue'),
        name: 'SystemInfo',
        meta: { title: '系统信息', icon: 'system' }
      }
    ]
  },
  {
    path: '/qms-help',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/qep/help.vue'),
        name: 'QmsHelp',
        meta: { title: '帮助中心', icon: 'question' }
      }
    ]
  },
  {
    path: '/api-test',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'index',
        component: apiTest,
        name: 'ApiTest',
        meta: { title: 'API测试', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/basic',
    component: Layout,
    redirect: 'noRedirect',
    name: 'BasicData',
    meta: { title: '基础数据管理', icon: 'dict', roles: ['admin'] },
    children: [
      // 产品管理
      {
        path: 'product',
        name: 'Product',
        component: () => import('@/views/qep/product/index'),
        meta: { title: '产品管理', icon: 'shopping' }
      },
      // 产品分类管理
      {
        path: 'category',
        name: 'Category',
        component: () => import('@/views/qep/category/index'),
        meta: { title: '产品分类', icon: 'tree' }
      },
      // 工序管理
      {
        path: 'process',
        name: 'Process',
        component: () => import('@/views/qep/process/index'),
        meta: { title: '工序管理', icon: 'tool' }
      },
      // 质量标准管理
      {
        path: 'standard',
        name: 'Standard',
        component: () => import('@/views/qep/standard/index'),
        meta: { title: '质量标准', icon: 'education' }
      },
      // 设备管理
      {
        path: 'equipment',
        name: 'Equipment',
        component: () => import('@/views/qep/equipment/index'),
        meta: { title: '设备管理', icon: 'monitor' }
      }
    ]
  },
  // 创建产品详情路由（不在菜单中显示）
  {
    path: '/product',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'detail',
        component: () => import('@/views/qep/product/detail'),
        name: 'ProductDetail',
        meta: { title: '产品详情', activeMenu: '/basic/product' }
      },
      {
        path: 'process',
        component: () => import('@/views/qep/product/process'),
        name: 'ProductProcess',
        meta: { title: '产品工序配置', activeMenu: '/basic/product' }
      },
      {
        path: 'standard',
        component: () => import('@/views/qep/product/standard'),
        name: 'ProductStandard',
        meta: { title: '产品标准配置', activeMenu: '/basic/product' }
      },
      {
        path: 'edit',
        component: () => import('@/views/qep/product/index'),
        name: 'ProductEdit',
        meta: { title: '编辑产品', activeMenu: '/basic/product' }
      }
    ]
  },
  // 标准详情路由（不在菜单中显示）
  {
    path: '/standard',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'detail',
        component: () => import('@/views/qep/standard/detail'),
        name: 'StandardDetail',
        meta: { title: '标准详情', activeMenu: '/basic/standard' }
      }
    ]
  },
  // 添加生产管理模块路由
  {
    path: '/production',
    component: Layout,
    redirect: 'noRedirect',
    name: 'ProductionManagement',
    meta: { title: '生产管理', icon: 'build', roles: ['admin'] },
    children: [
      // 批次管理
      {
        path: 'batch',
        name: 'BatchManagement',
        component: () => import('@/views/qep/production/batch/index'),
        meta: { title: '批次管理', icon: 'list' }
      },
      // 批次查询
      {
        path: 'batch/query',
        name: 'BatchQuery',
        component: () => import('@/views/qep/production/batch/query'),
        meta: { title: '批次查询', icon: 'search' }
      },
      // 工序流转统计
      {
        path: 'process/stats',
        name: 'ProcessStats',
        component: () => import('@/views/qep/production/process/stats'),
        meta: { title: '工序流转统计', icon: 'chart' }
      },
      // 生产进度监控
      {
        path: 'progress/monitor',
        name: 'ProgressMonitor',
        component: () => import('@/views/qep/production/progress/monitor'),
        meta: { title: '进度监控', icon: 'monitor' }
      }
    ]
  },
  // 添加质量检验模块路由
  {
    path: '/inspection',
    component: Layout,
    redirect: 'noRedirect',
    name: 'InspectionManagement',
    meta: { title: '质量检验', icon: 'checkbox', roles: ['admin'] },
    children: [
      // 检验任务
      {
        path: 'task',
        name: 'InspectionTask',
        component: () => import('@/views/qep/inspection/task/index'),
        meta: { title: '检验任务', icon: 'notebook' }
      },
      // 检验数据
      {
        path: 'data',
        name: 'InspectionData',
        component: () => import('@/views/qep/inspection/data/index'),
        meta: { title: '检验数据', icon: 'document' }
      },
      // 缺陷管理
      {
        path: 'defect',
        name: 'DefectManagement',
        component: () => import('@/views/qep/inspection/defect/index'),
        meta: { title: '缺陷管理', icon: 'warning' }
      },
      // 检验统计
      {
        path: 'statistics',
        name: 'InspectionStatistics',
        component: () => import('@/views/qep/inspection/statistics/index'),
        meta: { title: '检验统计', icon: 'chart' }
      }
    ]
  },
  // 检验任务详情路由（不在菜单中显示）
  {
    path: '/inspection/task',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'detail/:id',
        component: () => import('@/views/qep/inspection/task/detail'),
        name: 'InspectionTaskDetail',
        meta: { title: '检验任务详情', activeMenu: '/inspection/task' }
      }
    ]
  },
  // 检验数据详情路由（不在菜单中显示）
  {
    path: '/inspection/data',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'detail/:id',
        component: () => import('@/views/qep/inspection/data/detail'),
        name: 'InspectionDataDetail',
        meta: { title: '检验数据详情', activeMenu: '/inspection/data' }
      },
      {
        path: 'task/:taskId',
        component: () => import('@/views/qep/inspection/data/taskData'),
        name: 'TaskInspectionData',
        meta: { title: '任务检验数据', activeMenu: '/inspection/task' }
      }
    ]
  },
  // 批次详情路由（不在菜单中显示）
  {
    path: '/production/batch',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'detail',
        component: () => import('@/views/qep/production/batch/detail'),
        name: 'BatchDetail',
        meta: { title: '批次详情', activeMenu: '/production/batch' }
      }
    ]
  },
  // 工序流转记录路由（不在菜单中显示）
  {
    path: '/production/process',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'flow',
        component: () => import('@/views/qep/production/process/flow'),
        name: 'ProcessFlow',
        meta: { title: '工序流转记录', activeMenu: '/production/batch' }
      }
    ]
  },
  // 添加设备管理相关路由
  {
    path: '/equipment',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'detail',
        component: () => import('@/views/qep/equipment/detail'),
        name: 'EquipmentDetail',
        meta: { title: '设备详情', activeMenu: '/basic/equipment' }
      },
      {
        path: 'calibration',
        component: () => import('@/views/qep/equipment/calibration'),
        name: 'EquipmentCalibration',
        meta: { title: '设备校准记录', activeMenu: '/basic/equipment' }
      }
    ]
  },
  // 添加工序参数配置路由
  {
    path: '/process',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'params',
        component: () => import('@/views/qep/process/params'),
        name: 'ProcessParams',
        meta: { title: '工序参数配置', activeMenu: '/basic/process' }
      }
    ]
  },
  // 不合格品管理
  {
    path: '/nonconforming',
    component: Layout,
    redirect: 'noRedirect',
    name: 'NonconformingManagement',
    meta: { title: '不合格品管理', icon: 'tool' },
    children: [
      {
        path: 'nonconforming',
        name: 'Nonconforming',
        component: () => import('@/views/qep/nonconforming/index'),
        meta: { title: '不合格品记录', icon: 'clipboard' }
      },
      {
        path: 'repair-task',
        name: 'RepairTask',
        component: () => import('@/views/qep/repair-task/index'),
        meta: { title: '维修任务', icon: 'edit' }
      },
      {
        path: 'repair-evaluation',
        name: 'RepairEvaluation',
        component: () => import('@/views/qep/repair-evaluation/index'),
        meta: { title: '维修评价', icon: 'star' }
      },
      {
        path: 'repair-order',
        name: 'RepairOrder',
        component: () => import('@/views/qep/repair-order/index'),
        meta: { title: '维修工单', icon: 'form' }
      },
      {
        path: 'defect-record',
        name: 'DefectRecord',
        component: () => import('@/views/qep/defect-record/index'),
        meta: { title: '缺陷记录', icon: 'bug' }
      }
    ]
  }
]

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes = [
  {
    path: '/system/user-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:user:edit'],
    children: [
      {
        path: 'role/:userId(\\d+)',
        component: () => import('@/views/system/user/authRole'),
        name: 'AuthRole',
        meta: { title: '分配角色', activeMenu: '/system/user' }
      }
    ]
  },
  {
    path: '/system/role-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:role:edit'],
    children: [
      {
        path: 'user/:roleId(\\d+)',
        component: () => import('@/views/system/role/authUser'),
        name: 'AuthUser',
        meta: { title: '分配用户', activeMenu: '/system/role' }
      }
    ]
  },
  {
    path: '/system/dict-data',
    component: Layout,
    hidden: true,
    permissions: ['system:dict:list'],
    children: [
      {
        path: 'index/:dictId(\\d+)',
        component: () => import('@/views/system/dict/data'),
        name: 'Data',
        meta: { title: '字典数据', activeMenu: '/system/dict' }
      }
    ]
  },
  {
    path: '/monitor/job-log',
    component: Layout,
    hidden: true,
    permissions: ['monitor:job:list'],
    children: [
      {
        path: 'index/:jobId(\\d+)',
        component: () => import('@/views/monitor/job/log'),
        name: 'JobLog',
        meta: { title: '调度日志', activeMenu: '/monitor/job' }
      }
    ]
  },
  {
    path: '/tool/gen-edit',
    component: Layout,
    hidden: true,
    permissions: ['tool:gen:edit'],
    children: [
      {
        path: 'index/:tableId(\\d+)',
        component: () => import('@/views/tool/gen/editTable'),
        name: 'GenEdit',
        meta: { title: '修改生成配置', activeMenu: '/tool/gen' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  },
});

export default router; 